<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<%--
  Created by IntelliJ IDEA.
  User: Yipin
  Date: 2016/11/23
  Time: 13:15
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<jsp:include page="../include/head.jsp">
    <jsp:param name="title" value="数据备份管理"/>
</jsp:include>
<jsp:include page="../include/admin_header.jsp"/>

<jsp:include page="../include/admin_sidebar.jsp"/>

<div class="content-wrapper content-wrapper-bg">
    <section class="content-header">
        <h4 class="page-title" style="display: inline-block">
            <spring:message code="label.admin.data.backup.management"/>
        </h4>
        <a class="sie-head-add" href="/admin/dbManager/setting#backupSetting" style="margin-left:54px;">
            <spring:message code="label.admin.data.backup.set"/>
        </a>
        <a class="sie-head-blue" id="backupNow" style="margin-left:20px;width:inherit;min-width: 120px;max-width: 220px;">
            <spring:message code="label.admin.data.immediately.backup"/>
        </a>
    </section>
    <section class="content">
        <div class="row" style="margin: 0;">


            <div class="col-md-8" style="padding-left: 0;padding-right: 0;">
                <div style="margin-right: 20px">
                    <div id="backupInfo" class="box box-primary table-header-line">
                        <div class="box-body" style="padding: 0">
                            <table class="table" id="BACKUP_TABLE">
                                <thead>
                                <tr>
                                    <th><spring:message code="label.admin.data.backup.time"/></th>
                                    <th><spring:message code="label.admin.data.backup.type"/></th>
                                    <th><spring:message code="label.admin.data.fileOrFolder.name"/></th>
                                    <th><spring:message code="label.admin.data.backup.state"/></th>
                                </tr>
                                </thead>
                                <tbody>
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
            </div>
            <div class="col-md-4" style="padding-left:0;padding-right: 0">
                <div id="backupPlan"
                     style="border-top: 2px solid #41aaaa;">
                    <div class="box-body" style="height: 50px;text-align: center;background-color: #ebf0f5;">
                        <span style="font-size: 14px;color:#666;font-weight: 600;"><spring:message code="label.admin.current.data.backup.plan"/></span>
                    </div>
                </div>
            </div>
        </div>
    </section>
</div>
<jsp:include page="../include/foot.jsp"/>
<script>
    $(document).ready(function () {
        var table = $('#BACKUP_TABLE').DataTable({
            "dom": '<"datatable-header"<"dataTables_filter">l><"datatable-scroll"t><"datatable-footer dataTable-footer-padding"<"pull-left"i><"pull-right"p>>',
            paging: true,
            lengthChange: false,
            ordering: true,
            autoWidth: false,
            info: true,
            serverSide: true,
            fixedHeader: true,
            searching: true,
            "processing": true,
            aLengthMenu: [15],
            ajax: {
                url: "/admin/dbManager/api/v1/getDBBackupLogByDataTable",
                dataSrc: 'data'
            },
            columns: [
                {
                    data: 'backup_time'
                },
                {
                    data: 'backup_type'
                },
                {
                    data: 'backup_file'
                },
                {
                    data: 'backup_status'
                }
            ],
            "language": {url: '/lang/datatable.chs.json'}
        });

        $.ajax({
            url: "/admin/api/v1/getDBBackupPlan",
            success: function (data) {
                var backupPlan = data.backupPlan;
                if (backupPlan == null) {
                    $("#backupPlan").append($('<div class="backupInfo" style="padding-top: 90px;">暂无</div>'));
                } else {
                    var comType = backupPlan.complete.type == "week" ? '<spring:message code="label.week"/>' : '<spring:message code="label.month"/>';
                    var comValue = backupPlan.complete.value;
                    if (backupPlan.complete.type == "week") {
                        var weekValue = [];
                        $.each(comValue, function (index, value) {
                            weekValue.push(showWeek(value));
                        });
                        comValue = weekValue;
                    }
                    $("#backupPlan").append($('<div class="backupInfo"></div>')
                            .append($('<div id="type" style="padding-top: 40px"></div>')
                                    .append($('<span></span>').text('<spring:message code="label.type"/>:'))
                                    .append($('<span></span>').text(comType)))
                            .append($('<div style="padding-top:20px"></div>')
                                    .append($('<span></span>').text('<spring:message code="label.time"/>:'))
                                    .append($('<span></span>').text(comValue.toString() + " " + backupPlan.complete.time)))
                            .append($('<div style="padding-top:20px"></div>')
                                    .append($('<span></span>').text('<spring:message code="label.admin.data.backup.type.fullBackup"/>'))));

                    if (backupPlan.differences.value.length > 0) {
                        var diffValue = [];
                        $.each(backupPlan.differences.value, function (index, value) {
                            diffValue.push(showWeek(value));
                        });

                        $("#backupPlan").append($('<div class="backupInfo"></div>')
                                .append($('<div style="padding-top: 40px"></div>')
                                        .append($('<span></span>').text('<spring:message code="label.type"/>:'))
                                        .append($('<span></span>').text('<spring:message code="label.week"/>')))
                                .append($('<div style="padding-top:20px"></div>')
                                        .append($('<span></span>').text('<spring:message code="label.time"/>:'))
                                        .append($('<span></span>').text(diffValue.toString() + " " + backupPlan.differences.time)))
                                .append($('<div style="padding-top:20px"></div>')
                                        .append($('<span></span>').text('<spring:message code="label.admin.data.backup.type.differentialBackup"/>'))));
                    }

                    if (backupPlan.isEnable == false) {
                        $("#type").before($('<div style="padding-top: 20px;color:red"></div>').text('<spring:message code="label.admin.data.backup.plan.suspended"/>'));
                    }
                }
            }
        });

        $("#backupNow").click(function(){
            $.ajax({
                url:"/admin/api/v1/backupNow",
                success:function(data){
                    if(data.result == "SUCCESS"){
                        swal("SUCCESS",'<spring:message code="label.admin.data.backup.begin"/>',"success");
                    }else{
                        swal("Error",'<spring:message code="label.admin.data.immediately.backup.fail"/>',"error");
                    }
                }
            })
        })

    });

    function showWeek(data) {
        if (data == "MON") {
            return '<spring:message code="label.monday"/>';
        } else if (data == "TUE") {
            return '<spring:message code="label.tuesday"/>';
        } else if (data == "WED") {
            return '<spring:message code="label.wednesday"/>';
        } else if (data == "THU") {
            return '<spring:message code="label.thursday"/> ';
        } else if (data == "FRI") {
            return '<spring:message code="label.friday"/>';
        } else if (data == "SAT") {
            return '<spring:message code="label.saturday"/>';
        } else if (data == "SUN") {
            return '<spring:message code="label.sunday"/>';
        }
    }
</script>
